package com.hakim.wfb.platform.controller;

import com.hakim.wfb.customer.params.goods.CheckUserInvestingParameter;
import com.hakim.wfb.customer.result.goods.CheckUserInvestingView;
import com.hakim.wfb.customer.result.goods.GoodsListView;
import com.hakim.wfb.customer.service.GoodsService;
import com.hakim.wfb.customer.service.UserService;
import com.hkfs.fundamental.api.data.ListResultEx;
import com.hkfs.fundamental.api.data.ObjectResultEx;
import com.hkfs.fundamental.api.data.ResultEx;
import com.hkfs.fundamental.api.params.SessionIdentity;
import com.hkfs.fundamental.session.SessionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by lenovo on 2017/7/4.
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 用户是否投资中
     *
     * @param parameter
     * @param request
     * @return
     */
    @RequestMapping(value = "checkInvest", method = RequestMethod.POST)
    public ObjectResultEx<CheckUserInvestingView> checkInvest(CheckUserInvestingParameter parameter, HttpServletRequest request) {
        SessionHandler sessionHandler = new SessionHandler();
        SessionIdentity sessionIdentity = sessionHandler.getSessionIdentity(request, SessionIdentity.class);
        String userid = sessionIdentity.getUuid();
        parameter.setUserId(userid);
        return goodsService.checkInvest(parameter);
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ListResultEx<GoodsListView> getGoodsList() {
        ListResultEx<GoodsListView> res = goodsService.list();
        return res;
    }

    /**
     * 添加购物车
     *
     * @param goodsid
     * @param request
     * @return
     */
    @RequestMapping(value = "/addChart", method = RequestMethod.POST)
    public ResultEx addChart(String goodsid, HttpServletRequest request) {
        SessionHandler sessionHandler = new SessionHandler();
        SessionIdentity sessionIdentity = sessionHandler.getSessionIdentity(request, SessionIdentity.class);
        String userid = sessionIdentity.getUuid();
//        String userid = "sin7";
        return goodsService.addChart(goodsid, userid);
    }

    @RequestMapping(value = "/delChart", method = RequestMethod.POST)
    public ResultEx deleChart(String purchaseid, HttpServletRequest request) {
        SessionHandler sessionHandler = new SessionHandler();
        SessionIdentity sessionIdentity = sessionHandler.getSessionIdentity(request, SessionIdentity.class);
        String userid = sessionIdentity.getUuid();
//        String userid = "sin7";

        return goodsService.deleChart(purchaseid, userid);

    }
}
